import { Component, OnInit,Output,Input,EventEmitter,ViewChild } from '@angular/core';
import {Service} from '../../server/server';
import {HttpClient} from '@angular/common/http';
import {CudTableService} from '../../public/serve/cud-table/cud-table.service';
import {RemoveTableService} from '../../public/serve/remove-table/remove-table.service';
import {keywordTableAddtype} from '../../system/keyword-table-management/keywordtable.data';
import {InternalIndexedModalTableComponent} from '../internal-indexed-modal-table/internal-indexed-modal-table.component';
import {HintsService} from '../../public/serve/hints/hints.service';
import {InitService} from '../../public/serve/init/init.service';
import { ActivatedRoute,Params } from '@angular/router';
import {InternalIndexedModalRelthemeComponent} from '../internal-indexed-modal-reltheme/internal-indexed-modal-reltheme.component';
import {InternalIndexedModalTableVersionComponent} from '../internal-indexed-modal-table-version/internal-indexed-modal-table-version.component';
import {keyword} from '../../keyword/keyword.data';
declare var $:any;

@Component({
    selector: 'app-internal-indexed-table',
    templateUrl: './internal-indexed-table.component.html',
    styleUrls: ['./internal-indexed-table.component.css']
})
export class InternalIndexedTableComponent implements OnInit {

    @Input() public internalParam : any;
    public tabVal: any;
    public tabValAll:any;
    // relThemeDetail : object;
    internalModalTable : object;
    public internalMore: boolean = false;
    public internalindexeddeleteentEmit: any;
    public internalThemeWordsTablesRelEmit: any;
    public internalThemeWordsLeftEmit: any;
    currentThemeWordInfo : object;
    currentType = 1;
    // @Output()
    // relThemeDetailOutput: EventEmitter<any> = new EventEmitter();

    @Output()
    internalModalTableOutput: EventEmitter<any> = new EventEmitter();

    // 每一行的表格信息
    outputTabVal(tabval) {
        // this.relThemeDetailOutput.emit(tabval);
    }

    constructor(
        private server: Service,
        public request: CudTableService,
        private tableListService: CudTableService,
        private hintsService: HintsService,
        private removeTableService: RemoveTableService,
        public initService: InitService,
        private http: HttpClient,
        private activatedRoute: ActivatedRoute,
        ) { }

    ngOnInit() {
        // this.getIndexedTables();
        this.internalindexeddeleteentEmit = this.server.internalindexeddeleteentEmit.subscribe((value: any) => {
            if (value) {
                this.getIndexedTables();
            }
        });
        this.internalThemeWordsLeftEmit = this.server.internalThemeWordsLeftEmit.subscribe((value: any) => {
            if (value) {
                this.currentThemeWordInfo = value
            }
        });
        this.internalThemeWordsTablesRelEmit = this.server.internalThemeWordsTablesRelEmit.subscribe((value: any) => {
            if (value == 2) {
                //关联主题词查询
                this.currentType = 2;
                this._getRelThemeList();
            }else{
                this.currentType = 1;
                this.getIndexedTables();
            }
        });



        let obj = this;
        //关闭弹窗除非
        $('#viewRelTheme').on('hide.bs.modal', function (e) {
            obj.server.internalindexeddeleteentEmit.emit(true);
        });
    }
    ngOnDestroy() {
        this.internalThemeWordsTablesRelEmit ? this.internalThemeWordsTablesRelEmit.unsubscribe() : '';
        this.internalThemeWordsLeftEmit ? this.internalThemeWordsLeftEmit.unsubscribe() : '';
        this.internalindexeddeleteentEmit ? this.internalindexeddeleteentEmit.unsubscribe() : '';
    }

    public paramData:any;

    /**
     * 查询标引主题词列表
     */
    public getIndexedTables(){
        let url = this.server.baseUrl + 'Service/IdxProduct/getProductThemeWordsList';
        let data = {
            "token": "b25dbd7138cf48e09941087e6d9a2e93",
            // "checkStatus":1,
            "productId":this.internalParam['productId'],
            "taskId":this.internalParam['taskId'],
            "themeTableId":this.internalParam['themeTableId'],
            "id":"",
            "pageNo":"",
            "pageSize":"",
            "startTime":"",
            "endTime":"",
            "themeWordsName":""
        };
        this.request.postRequest(url,data).then((res:any) => {
            if (res.result_code === '0000') {
                this.tabVal = res.result;
            }else{
                // this.hintsService._operationHints(res.message,res.result_code);
            }

            this.getIndexedTablesAll();
            // this._cancelChecked();
            return;
        })
            .catch((err: any)=>{
                // this.hintsService._operationHints("error","error");
                // this._cancelChecked();
                return;
            });
    }

/**
     * 查询所有已经标引主题词列表，不带分页
     */
    public getIndexedTablesAll(){
        let url = this.server.baseUrl + 'Service/IdxProduct/getProductThemeWordsList';
        let data = {
            "token": "b25dbd7138cf48e09941087e6d9a2e93",
            // "checkStatus":1,
            "productId":this.internalParam['productId'],
            "taskId":this.internalParam['taskId'],
            "themeTableId":this.internalParam['themeTableId'],
            "id":"",
            "pageNo":1,
            "pageSize":9999,
        };
        this.request.postRequest(url,data).then((res:any) => {
            if (res.result_code === '0000') {
                this.tabValAll = res.result;
            }
            return;
        })
        .catch((err: any)=>{
            return;
        });
    }

    //根据标引主题词和表获取已关联主题词列表
    _getRelThemeList(){

        if(this.currentThemeWordInfo){
            let data = {
                // pageNo        : this.pageNumber,
                // pageSize      : this.pageSize,
                pageNo        : 1,
                pageSize      : 999,
                queryCriteria : keyword.retrieval.queryCriteria,
                themeTableId  :  this.currentThemeWordInfo['themeTableId'], // 左侧主题词表id
                refThemeTableId  :  this.internalParam['themeTableId'], // 当前主题词表id
                themeWordsId  :  this.currentThemeWordInfo['themeWordsId'],    //左侧主题词id
                productId  : this.internalParam['productId'],
                taskId  : this.internalParam['taskId']
            };
            let url = this.server.baseUrl +  'Service/IdxProduct/getProductThemeRelationList';
            this.request.postRequest(url,data).then((res:any) => {
                if (res.result_code === '0000') {
                    this.tabVal = res.result;
                }else{
                    this.hintsService.messages(res.message,res.result_code);
                }
                return;
            })
                .catch((err: any)=>{
                    this.hintsService.messages("error","error");
                    return;
                });
        }
    }




    //导入table
    @ViewChild('table')
    table: InternalIndexedModalTableComponent;

    /**
     * 删除（弹窗）
     * @param info
     */
    _delete(id,theme_id) {
        this.server.internalDeleteThemeIdEmit.emit(theme_id);
        this.removeTableService.keywordRemoveChose(true,id,1);
        $("#delete").modal("show");
    }

    /**
     * 删除（弹窗）
     * @param info
     */
    _deleteRel(id) {
        this.removeTableService.keywordRemoveChose(true,id,1);
        $("#reltheme-delete").modal("show");
    }

    ParentDelete(result_code: string) {
        this.getIndexedTables();
    }

    _info(val){
        this.server.internalIndexedRelThemeEmit.emit(val);
    }


}
